package com.eva.dict.loaders;

import com.eva.dao.system.model.SystemDictData;
import com.eva.dict.dao.SystemDictMapper;
import com.eva.framework.cache.CacheProxy;
import com.eva.framework.common.constants.ResponseStatus;
import com.eva.framework.common.exception.BusinessException;
import com.eva.dict.vo.SystemDictWithDataVO;
import com.eva.framework.dict.SystemDictHelper;
import com.eva.framework.dict.model.SystemDictCache;
import com.eva.framework.dict.model.SystemDictDataCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * v5.1.0整体调整
 */
@Slf4j
@Component
public class SystemDictCacheLoader {

    @Resource
    private SystemDictMapper systemDictMapper;

    @Resource
    private CacheProxy<String, Map<String, SystemDictCache>> cacheProxy;

    /**
     * 加载到缓存
     *
     * @param force 是否强制加载
     */
    public void loadToCache (boolean force) {
        this.loadToCache(null, force);
    }

    /**
     * 加载到缓存
     *
     * @param dataScope 数据作用域，取值为application-config.yml中的id，为空时将自动读取id值
     * @param force 是否强制加载
     */
    public void loadToCache (String dataScope, boolean force) {
        // 获取缓存键
        SystemDictHelper dictHelper = SystemDictHelper.build(dataScope);
        String cacheKey = dictHelper.getCacheKey();
        log.info("开始加载 {} 字典数据至缓存", dictHelper.getDataScope());
        if (!force && cacheProxy.get(cacheKey) != null) {
            log.debug("系统字典数据已加载至缓存，跳过本次加载！");
            return;
        }
        // 根据作用域查询字典&字典数据
        List<SystemDictWithDataVO> dictWithDataList = systemDictMapper.selectListWithData(dictHelper.getDataScope());
        // 转换为缓存对象列表
        List<SystemDictCache> dictList = new ArrayList<>();
        for (SystemDictWithDataVO dictWithDataVO : dictWithDataList) {
            SystemDictCache dictCache = new SystemDictCache();
            BeanUtils.copyProperties(dictWithDataVO, dictCache, "dataList");
            List<SystemDictDataCache> dataList = new ArrayList<>();
            for (SystemDictData data : dictWithDataVO.getDataList()) {
                SystemDictDataCache dictDataCache = new SystemDictDataCache();
                BeanUtils.copyProperties(data, dictDataCache);
                dataList.add(dictDataCache);
            }
            dictCache.setDataList(dataList);
            dictList.add(dictCache);
        }
        // 转为map对象
        Map<String, SystemDictCache> dictMap = new HashMap<>();
        for (SystemDictCache dictCache : dictList) {
            if (dictMap.containsKey(dictCache.getCode())) {
                throw new BusinessException(ResponseStatus.DATA_ERROR, "字典中存在重复的code，请检查字典数据！");
            }
            dictMap.put(dictCache.getCode(), dictCache);
        }
        // 缓存数据
        cacheProxy.put(cacheKey, dictMap);
        log.info("字典数据缓存完成，共加载 {} 条字典数据", dictMap.size());
    }
}
